package net.yla.smartbus.module.query;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import net.yla.smartbus.core.util.date.DateMaxAndMin;
import net.yla.smartbus.core.util.date.FirstDayAndLastDayInMonth;
import net.yla.smartbus.core.util.date.YesterdayAndTomorrow;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author : yang lei
 * @version : 1.0
 * @date : Created in 13:41 2020/11/11
 * @description :  生成运营报表统一查询参数
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class QueryString {

    /**
     * 调用方标识号
     */
    private String sid;

    /**
     * 请求签名
     */
    private String sign;

    /**
     * 报表日期
     */
    private String date;
    private Date startTime;
    private Date endTime;

    /**
     * 报表名称
     */
    private String bookName;


    //统计时间纬度
    /**
     * 0自定义日期段；
     * 1日；
     * 2周；
     * 3月；
     * 4季度；
     * 5年度；
     */
    private Integer type;

    //操作类型
    /**
     * 1生成；
     * 2更新；
     * 3删除
     */
    private Integer op;

    private Integer alarmType;

    private Integer Id;


    public Date getStartTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date parse = null;

        if (date == null) {
            Date date = new Date();
            Date yesterday = YesterdayAndTomorrow.yesterday(date);

            if (type == 1) {
                parse = DateMaxAndMin.getMintDay(yesterday);
            }

            if (type == 3) {
                try {
                    Map<String, Date> stringDateMap = FirstDayAndLastDayInMonth.returnDate(date);
                    Date date2 = stringDateMap.get("first");
                    Date yesterday1 = YesterdayAndTomorrow.yesterday(date2);
                    Map<String, Date> stringDateMap1 = FirstDayAndLastDayInMonth.returnDate(yesterday1);
                    Date date3 = stringDateMap1.get("first");
                    parse = date3;
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        } else {
            if (type == 1) {
                try {
                    parse = format.parse(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

            if (type == 3) {
                try {
                    parse = format.parse(date);
                    Map<String, Object> stringObjectMap = FirstDayAndLastDayInMonth.convertMonthByDate(parse);
                    Object first = stringObjectMap.get("first");
                    parse = format.parse((String) first);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        return DateMaxAndMin.getMintDay(parse);
    }


    public Date getEndTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;

        if (date == null) {
            Date date = new Date();
            Date yesterday = YesterdayAndTomorrow.yesterday(date);

            if (type == 1) {
                parse = DateMaxAndMin.getMaxDay(yesterday);
            }

            if (type == 3) {
                try {
                    Map<String, Date> stringDateMap = FirstDayAndLastDayInMonth.returnDate(date);
                    Date date2 = stringDateMap.get("first");
                    Date yesterday1 = YesterdayAndTomorrow.yesterday(date2);
                    Map<String, Date> stringDateMap1 = FirstDayAndLastDayInMonth.returnDate(yesterday1);
                    Date date3 = stringDateMap1.get("last");
                    parse = date3;
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        } else {
            if (type == 1) {
                try {
                    parse = format.parse(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

            if (type == 3) {
                try {
                    Date parse1 = format.parse(date);
                    parse = parse1;
                    Map<String, Object> stringObjectMap = FirstDayAndLastDayInMonth.convertMonthByDate(parse);
                    Object last = stringObjectMap.get("last");
                    parse = format.parse((String) last);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        return DateMaxAndMin.getMaxDay(parse);
    }

}
